/*
 * @Author: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @Date: 2024-03-30 18:01:42
 * @LastEditTime: 2024-04-02 11:24:50
 * @FilePath: \electron-wallpaper\src\preload\index.js
 * @Description:
 *
 */
import { electronAPI } from '@electron-toolkit/preload'
import { contextBridge, ipcRenderer } from 'electron'

// Custom APIs for renderer
const api = {
  setWallpaper: (imagePath) => {
    return ipcRenderer.invoke('wallpaper:setWallpaper', imagePath)
  },
  selectWallpaper: () => {
    return ipcRenderer.invoke('wallpaper:selectWallpaper')
  },
  loopWallpaper: (state, interval) => {
    return ipcRenderer.invoke('wallpaper:loopWallpaper', state, interval)
  },
  getWallpaperList: () => {
    return ipcRenderer.invoke('wallpaper:getWallpaperList')
  },
  winQuit: () => {
    return ipcRenderer.invoke('win:quit')
  },
  openExternal: (url) => {
    return ipcRenderer.send('openExternal', url)
  },
  winMinimize: () => {
    return ipcRenderer.send('win:minimize')
  },
  winToggleTop: () => {
    return ipcRenderer.send('win:toggleTop')
  }
}

// Use `contextBridge` APIs to expose Electron APIs to
// renderer only if context isolation is enabled, otherwise
// just add to the DOM global.
if (process.contextIsolated) {
  try {
    contextBridge.exposeInMainWorld('electron', electronAPI)
    contextBridge.exposeInMainWorld('api', api)
  } catch (error) {
    console.error(error)
  }
} else {
  window.electron = electronAPI
  window.api = api
}
